import { _decorator, Component, instantiate, Label, Layout, Prefab, UITransform, Node } from 'cc';
import { GameData, GameTools } from './GameData';
import { AdvertType, gameEvent, GameEventType, GameStorage, GameTimer } from './Global';
import { Code } from './Code';
import { WXUtils } from './WXUtils';
import { SearchCount } from './SearchCount';
const { ccclass, property } = _decorator;

@ccclass('CodeList')
export class CodeContent extends Component {

    @property({ type: Prefab })
    CodePrefab: Prefab = null;

    // 放大镜旁边的数字
    @property({ type: Node })
    SearchCountNode: Node = null;

    protected onLoad(): void {
        gameEvent.on(GameEventType.ADVERT, this.playAdvert, this);
    }

    onDisable() {
        gameEvent.off(GameEventType.ADVERT, this.playAdvert, this);
    }

    start() {
        this.restart();
    }

    update(deltaTime: number) {

    }

    // 重启游戏
    restart() {
        // 先计算一下文字区的宽度
        const baseNumber = Math.sqrt(GameData.codeLib.length)
        const transform = this.node.getComponent(UITransform);
        transform.setContentSize(baseNumber * 110 + 10, 0);
        const layout = this.node.getComponent(Layout);
        layout.constraintNum = baseNumber;



        this.node.removeAllChildren();
        GameData.getCodeLib().split('').forEach((char, index) => {
            const codeNode = instantiate(this.CodePrefab);
            const label = codeNode.getChildByName('Label').getComponent(Label)
            label.string = char;
            this.node.addChild(codeNode);
        });
    }

    // 找到下一个要点击的字符
    findNextCode() {
        // 先判断下道具数量是否足够
        if (GameTools.searchCount <= 0) {
            // 道具不足, 弹出广告
            if (GameTools.getSearchMaxCount <= 0) {
                // 本关获取上限 5 次
                WXUtils.showToast('本关上限5次');
                return
            }
            WXUtils.showVideoAd();
            return;
        }

        const nextCode = GameData.getNextCode();
        const nodeArr = this.node.children;

        for (let i = 0; i < nodeArr.length; i++) {
            const node = nodeArr[i];
            const label = node.getChildByName('Label').getComponent(Label);
            // console.log('xxxxxxxxxxx');

            if (label.string === nextCode) {
                const code = node.getComponent(Code);
                if (code.status === 'none') {
                    // 点击一下
                    code.onClick();
                    GameTools.searchCount -= 1;
                    break;
                }
            }
        }
    }
    private playAdvert(advertType: AdvertType) {
        if (advertType === AdvertType.AD_SHOW) {
            // 打开广告，计时暂停
            GameTimer.pause();
        }
        else if (advertType === AdvertType.AD_ERROR) {
            GameTimer.play();
            console.log('广告加载失败');
        } else if (advertType === AdvertType.AD_SUCCESS) {
            GameTimer.play();
            // 道具数量+1
            GameTools.searchCount += 1;
            // 本关获取次数 -1
            GameTools.getSearchMaxCount--;
            // 刷新道具数量
            this.SearchCountNode.getComponent(SearchCount).refreshCount();
        } else if (advertType === AdvertType.AD_CLOSE) {
            console.log('广告关闭');
            GameTimer.play();
        }
    }
}


